<!-- 我的弹窗 -->

<template>
  <WebModal word="布控摄像头" passTitle="创建人脸库" passTip="确定创建此设备的人脸库吗？" small>
    <WebForm>
      <FormSlot label="选择设备" bind="deviceId" tip="请选择设备" required>
        <span class="el-button--text" @click="toOpen">
          {{ one.deviceName }}
        </span>

        <span v-if="!isShow">
          <BtnText v-if="one.deviceId" title="清除" @click="toClear" />
          <BtnPrimary v-else title="选择" @click="toOpen" />
        </span>
      </FormSlot>

      <FormSelect label="设备类别" bind="deviceCategoryId" :dict="categoryDict" disabled />

      <FormSelect label="设备类型" bind="deviceTypeId" :dict="typeDict" disabled />

      <FormTree
        :label="`所在${CORE.MAP.REGION}`"
        bind="regionId, regionName"
        :dict="CORE.REGION.DICT"
        allName
        disabled
      />
    </WebForm>

    <EasyModal v-model="easyVisible" title="选择摄像头" medium @submit="toSubmit" @close="toReset">
      <ChooseIOTOne :obj="obj" bind="device" deviceCategoryId="1" disableCategory />
    </EasyModal>
  </WebModal>
</template>

<script>
// ============================== 导入依赖 ============================== //

import CORE from '@/CORE'
import FormSync from '@admin/views/System/IOT/IOTList/MyModal/FormSync'

// ============================== 导出组件 ============================== //

export default {
  /**
   * 名称定义 (实际使用名称)
   */
  name: 'MyModal',

  /**
   * 混入列表 (配置选项合并)
   */
  mixins: [CORE.mixWeb()],

  /**
   * 组件导入 (Abc.vue + Abc/index.vue)
   */
  ...CORE.components(require.context('./', true, /vue$/)),

  /**
   * 状态数据 (函数返回对象)
   */
  data() {
    return {
      easyVisible: false, // 弹窗是否可见
      obj: { device: {} }, // 数据对象
    }
  },

  /**
   * 计算属性 (慎用箭头函数)
   */
  computed: FormSync.computed,

  /**
   * 监听属性 (深度 deep 首次 immediate)
   */
  watch: {
    /**
     * 监听弹窗可见
     */
    easyVisible(visible) {
      visible && this.toReset()
    },
  },

  /**
   * 本地方法 (轻量重要在前)
   */
  methods: {
    /**
     * 弹窗
     */
    toOpen() {
      this.easyVisible = true
    },

    /**
     * 提交
     */
    toSubmit() {
      if (CORE.isEmpty(this.obj.device)) {
        CORE.MSG.warning('请选择选择摄像头')
        return
      }

      const { device } = this.obj

      Object.assign(this.one, {
        deviceIds: [device.id],
        deviceId: device.id,
        deviceName: device.deviceName,
        deviceCategoryId: device.deviceCategoryId,
        deviceTypeId: device.deviceTypeId,
        regionId: device.areaId,
      })

      this.easyVisible = false
    },

    /**
     * 清除
     */
    toClear() {
      this.setOne()
      this.obj = { device: {} }
    },

    /**
     * 重置
     */
    toReset() {
      const { one } = this
      const device = {
        id: one.deviceId,
        deviceName: one.deviceName,
        deviceCategoryId: one.deviceCategoryId,
        deviceTypeId: one.deviceTypeId,
        areaId: one.regionId,
      }
      this.obj = { device }
    },
  },
}
</script>
